package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.LikeRecord;
import com.dzj.fishingcommunity.service.LikeRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/like-records")
@Tag(name = "点赞记录管理", description = "点赞记录管理相关的接口")
public class LikeRecordController {

    @Autowired
    private LikeRecordService likeRecordService;

    @Operation(summary = "获取所有点赞记录")
    @GetMapping
    public ResponseResult<List<LikeRecord>> getAllLikeRecords(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<LikeRecord> page = new Page<>(current, size);
        Page<LikeRecord> likeRecordPage = likeRecordService.page(page);
        return ResponseResult.success(likeRecordPage.getRecords());
    }

    @Operation(summary = "根据ID获取点赞记录")
    @GetMapping("/{postId}/{userId}")
    public ResponseResult<LikeRecord> getLikeRecordById(@PathVariable Integer postId, @PathVariable Integer userId) {
        LikeRecord likeRecord = likeRecordService.getOne(new QueryWrapper<LikeRecord>().eq("post_id", postId).eq("user_id", userId));
        return ResponseResult.success(likeRecord);
    }

    @Operation(summary = "新增点赞记录")
    @PostMapping
    public ResponseResult<LikeRecord> addLikeRecord(@RequestBody LikeRecord likeRecord) {
        likeRecordService.save(likeRecord);
        return ResponseResult.success(likeRecord);
    }

    @Operation(summary = "更新点赞记录")
    @PutMapping("/{postId}/{userId}")
    public ResponseResult<LikeRecord> updateLikeRecord(@PathVariable Integer postId, @PathVariable Integer userId, @RequestBody LikeRecord likeRecord) {
        likeRecord.setPostId(postId);
        likeRecord.setUserId(userId);
        likeRecordService.updateById(likeRecord);
        return ResponseResult.success(likeRecord);
    }

    @Operation(summary = "删除点赞记录")
    @DeleteMapping("/{postId}/{userId}")
    public ResponseResult<Void> deleteLikeRecord(@PathVariable Integer postId, @PathVariable Integer userId) {
        boolean isDeleted = likeRecordService.remove(new QueryWrapper<LikeRecord>().eq("post_id", postId).eq("user_id", userId));
        if (!isDeleted) {
            return ResponseResult.error(400, "点赞记录删除出现异常");
        }
        return ResponseResult.success(null);
    }


}